package com.aigou.admin.dao.impl;

import com.aigou.admin.utils.JDBCUtils;
import com.aigou.admin.dao.ProductDao;
import com.aigou.admin.entity.Product;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Hor
 * @Date: 2024/11/29 15:24
 * @Version: 1.0
 */

@Repository
public class ProductDaoImpl implements ProductDao {
    @Override
    public List<Product> selectAll() {
        return JDBCUtils.executeQuery(Product.class, "select * from s_product");
    }

    @Override
    public Product selectById(String id) {
        List<Product> users = JDBCUtils.executeQuery(Product.class, "select * from s_product where id = ? ",  id);
        if (users.isEmpty()) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public List<Product> selectByName(String product_name) {
        return JDBCUtils.executeQuery(Product.class, "select * from s_product where product_name like ?", "%" + product_name + "%");
    }

    @Override
    public Boolean insert(Product product) {
        int rows = JDBCUtils.executeUpdate("insert into s_product(" +
                "id," +
                "product_name," +
                "product_image," +
                "price," +
                "product_type," +
                "product_desc," +
                "create_time," +
                "product_brand" +
                ") values(?,?,?,?,?,?,?,?)",
                product.getId(),
                product.getProduct_name(),
                product.getProduct_image(),
                product.getPrice(),
                product.getProduct_type(),
                product.getProduct_desc(),
                product.getCreate_time(),
                product.getProduct_brand()
                );
        return rows > 0;
    }

    @Override
    public Boolean update(Product product) {
        int rows = JDBCUtils.executeUpdate("update s_product set " +
                "product_name =?," +
                "product_image=?," +
                "price=?," +
                "product_type=?," +
                "product_desc=?," +
                "product_brand=?" +
                "where id = ?",
                product.getProduct_name(),
                product.getProduct_image(),
                product.getPrice(),
                product.getProduct_type(),
                product.getProduct_desc(),
                product.getProduct_brand(),
                product.getId()
                );
        return rows > 0;
    }

    @Override
    public Boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_product where id = ?", id);
        return rows >0;
    }

    @Override
    public Integer selectProductCount() {
        return JDBCUtils.executeCount("select count(*) from s_product");
    }

    @Override
    public List<Product> selectProductByLimit(Integer start, Integer limit) {
        return JDBCUtils.executeQuery(Product.class, "select * from s_product limit ?,?", start, limit);
    }
}
